<?php
namespace App\Http\Controllers\Company;

use Illuminate\Http\Request;
use App\User;
/**
 *
 * @author Joker
 *        
 */
class AccountController extends CompanyController
{
    public function index()
    {
        return view('company.account');
    }
    
    public function accountPassword()
    {
        return view('company.accountpwd');
    }
    /**
     *  用户邮箱修改、密码修改、账户基本资料修改
     * @param Request $req
     */
    public function store(Request $req)
    {
        $data = $req->only('username','email','sex','phone','real_name','qq','signature','birth','password','oldpwd','repassword','oldemail','email','types');
        $curruser = User::find($this->uid);
        switch($data['types'])
        {
            case 'eamil':
                if(empty(trim($data['oldemail'])))
                {
                    return redirect()->intended('/member/password')->withErrors(['oldemail' => '原邮箱不能为空']);
                }
                elseif($curruser->email !== bcrypt(trim($data['oldemail'])))
                {
                    return redirect()->intended('/member/password')->withErrors(['oldpwd' => '邮箱输入错误！']);
                }
                if(empty(trim($data['phone'])))
                {
                    return redirect()->intended('/member/password')->withErrors(['phone' => '手机号不能为空！']);
                }
                elseif(trim($data['phone']) !== $curruser->phone)
                {
                    return redirect()->intended('/member/password')->withErrors(['phone' => '手机好嘛不能正确！']);
                }
                $curruser->email = trim($data['email']);
                return redirect('/member/email');
                break;
            case 'pwd':
                if(empty(trim($data['oldpwd'])))
                {
                    return redirect()->intended('/member/password')->withErrors(['oldpwd' => '原始密码不能为空']);
                }
                elseif($curruser->password !== bcrypt(trim($data['oldpwd'])))
                {
                    return redirect()->intended('/member/password')->withErrors(['oldpwd' => '原密码错误！']);
                }
                if(empty(trim($data['password']) || empty(trim($data['repassword']))))
                {
                    return redirect()->intended('/member/password')->withErrors(['pwd' => '原密码不能为空！']);
                }
                elseif(trim($data['password']) !== trim($data['repassword']))
                {
                    return redirect()->intended('/member/password')->withErrors(['pwd' => '两次输入的不一致！']);
                }
                if(empty(trim($data['phone'])))
                {
                    return redirect()->intended('/member/password')->withErrors(['phone' => '手机号不能为空！']);
                }
                elseif(trim($data['phone']) !== $curruser->phone)
                {
                    return redirect()->intended('/member/password')->withErrors(['phone' => '手机好嘛不能正确！']);
                }
                $curruser->password = bcrypt(trim($data['password']));
                return redirect('/member/password');
                break;
            case 'info':
                if(trim($data['username']))
                    $curruser->name = trim($data['username']);
                    if(trim($data['sex']))
                        $curruser->sex = intval($data['sex']);
                    if(trim($data['real_name']))
                        $curruser->real_name = trim($data['real_name']);
                    if(trim($data['qq']))
                        $curruser->qq = intval($data['qq']);
                    if(trim($data['signature']))
                        $curruser->signature = trim($data['signature']);
                    if(trim($data['birth']))
                    {
                        $birth = explode("-", $data['birth']);
                        $curruser->birth_year = $birth[0];
                        $curruser->birth_month = $birth[1];
                    }
                    $curruser->save();
                    return redirect('/member/setting');
                    break;
        }
    
    }
}